package com.qzy.reggie.controller;

import com.qzy.reggie.common.PageResult;
import com.qzy.reggie.common.Result;
import com.qzy.reggie.pojo.Employee;
import com.qzy.reggie.service.LppEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class LppEmployeeController {

    @Autowired
    private LppEmployeeService employeeService;

    @GetMapping()
    public Result list(Integer page,Integer pageSize,String name){
//        List<Employee> employees = employeeService.queryEmployeeList();


//        Map<String,Object> result = new HashMap<>();
//        result.put("data",employees);
//        result.put("code",200);
//        result.put("msg","查询成功");
//        return result;
        PageResult pageResult = employeeService.queryEmployeeList(page,pageSize,name);

        return Result.success("查询成功",pageResult);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Employee emp, HttpSession session){
//        Map<String,Object> result = new HashMap<>();


        Employee employee = employeeService.queryEmployeeByUserName(emp.getUsername());

        if (employee == null){
//            result.put("code",400);
//            result.put("msg","登录失败：用户名错误");
//            return result;
            return Result.error("登陆失败：用户名错误");
        }

        String pwd = employee.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(emp.getPassword().getBytes());

        if (!pwd.equals(md5password)){
//            result.put("code",400);
//            result.put("msg","登录失败：密码错误");
//            return result;
            return Result.error("登陆失败：密码错误");
        }

        if (employee.getStatus() != 1){
//            result.put("code",400);
//            result.put("msg","登录失败：该员工已经禁用");
//            return result;
            return Result.error("登陆失败：该员工已经禁用");
        }

        session.setAttribute("empId",employee.getId());

//        result.put("data",employee);
//        result.put("code",1);
//        result.put("msg","登录成功");
//        return result;
        return Result.success("登陆成功",employee);

    }

    @PostMapping("/logout")
    public Result logout(HttpSession session){
//        Map<String,Object> result = new HashMap<>();
        session.removeAttribute("empId");

//        result.put("data",null);
//        result.put("code",1);
//        result.put("msg","退出成功");
//        return result;
        return Result.success("退出成功");
    }

    @PostMapping()
    public Result save(@RequestBody Employee employee,HttpSession session){
        Long empId = (Long) session.getAttribute("empId");

        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        Integer ret = employeeService.save(employee);

        if(ret>0){
            return Result.success("新增成功");
        }else{
            return Result.success("新增失败");
        }
    }

    @GetMapping("{id}")
    public Result getEmpById(@PathVariable Long id){
        return Result.success(employeeService.queryEmployeeById(id));
    }
    @PutMapping()
    public Result updateEmp(@RequestBody Employee employee,HttpSession session){
        Long empId = (Long) session.getAttribute("empId");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);

        Integer ret = employeeService.update(employee);

        if(ret > 0){
            return Result.success("修改成功");

        }else {
            return Result.error("修改失败");
        }
    }
}
